package com.yanqu.road.server.cmd.activity.autoreward.rank;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.cross.CrossGeneralActivityUnionRankModel;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.activity.CrossGeneralActivityRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;

import java.math.BigInteger;
import java.util.*;

public abstract class BaseCrossRank {

    protected int activityId;

    public BaseCrossRank(int activityId) {
        this.activityId = activityId;
    }

    public abstract AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg();
    public abstract AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg();

    public AutoSendRewardProto.CrossUserRankMsg.Builder getCrossUserRankMsg(int groupId) {
        return null;
    }
    public AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossUnionRankMsg(int groupId){
        return null;
    }

    void addUserRankData(AutoSendRewardProto.CrossUserRankMsg.Builder msg, int conditionType, long userId, int rank) {
        AutoSendRewardProto.UserRankMsg.Builder builder = AutoSendRewardProto.UserRankMsg.newBuilder();
        builder.setConditionType(conditionType);
        builder.setUserId(userId);
        builder.setRank(rank);
        msg.addData(builder);
    }

    void addUnionRankData(AutoSendRewardProto.CrossUnionRankMsg.Builder msg, int conditionType, String unionUid, int rank) {
        AutoSendRewardProto.UnionRankMsg.Builder builder = AutoSendRewardProto.UnionRankMsg.newBuilder();
        builder.setConditionType(conditionType);
        builder.setUnionId(unionUid);
        builder.setRank(rank);

        Set<Integer> unionConditionTypeSet = new HashSet<>();
        unionConditionTypeSet.add(eGamePlayerEventType.UnionEarnSpeedRiseRank.getValue());
        unionConditionTypeSet.add(eGamePlayerEventType.UnionIntimacyRiseRank.getValue());
        unionConditionTypeSet.add(eGamePlayerEventType.UnionExpRiseRank.getValue());
        unionConditionTypeSet.add(eGamePlayerEventType.CrossTradeWarUnionScoreRiseRank.getValue());
        if (unionConditionTypeSet.contains(conditionType)) {
            conditionType = 0;
        }

        UnionActivityConditionData conditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(msg.getActivityId(), conditionType, unionUid);
        if (conditionData != null) {
            for (Map.Entry<Long, Integer> entry : conditionData.getMemberPositionMap().entrySet()) {
                builder.addPosList(CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg.newBuilder().setUserId(entry.getKey()).setPos(entry.getValue()));
            }
        }

        msg.addData(builder);
    }

    void addUnionRankWithUnionInfoData(AutoSendRewardProto.CrossUnionRankWithUnionInfoMsg.Builder msg, int conditionType, String unionUid, int rank, long masterId, List<Long> memberList) {
        AutoSendRewardProto.UnionRankWithUnionInfoMsg.Builder builder = AutoSendRewardProto.UnionRankWithUnionInfoMsg.newBuilder();
        builder.setConditionType(conditionType);
        builder.setUnionId(unionUid);
        builder.setRank(rank);
        builder.setMasterId(masterId);
        builder.addAllMemberId(memberList);
        msg.addData(builder);
    }

    AutoSendRewardProto.CrossUserRankMsg.Builder getCrossNormalActivityUserRankMsg() {
        AutoSendRewardProto.CrossUserRankMsg.Builder msg = AutoSendRewardProto.CrossUserRankMsg.newBuilder();
        msg.setActivityId(activityId);
        Map<Integer, CrossActivityRankListModel> modelMap = ActivityRankMgr.getCrossActivityNewRankListModelMap(activityId);
        if (modelMap != null) {
            for (Map.Entry<Integer, CrossActivityRankListModel> entry : modelMap.entrySet()) {
                CrossActivityRankListModel rankListModel = entry.getValue();
                int conditionType = rankListModel.getConditionType();
                int rank = 0;
                for (CrossUserRank rankData : rankListModel.getRankList()) {
                    long userId = rankData.getUserId();
                    rank++;
                    //怪化猫 试炼排行0分不给獎勵
                    if(conditionType == eGamePlayerEventType.CatActivityDamageRank.getValue()){
                        if(rankData.getValue().compareTo(BigInteger.ZERO) == 0){
                            continue;
                        }
                    }
                    addUserRankData(msg, conditionType, userId, rank);
                }
            }
        }
        return msg;
    }

    AutoSendRewardProto.CrossUnionRankMsg.Builder getCrossNormalActivityUnionRankMsg() {

        AutoSendRewardProto.CrossUnionRankMsg.Builder msg = AutoSendRewardProto.CrossUnionRankMsg.newBuilder();
        msg.setActivityId(activityId);
        HashMap<Integer, CrossGeneralActivityUnionRankModel> model = CrossGeneralActivityRankMgr.getCrossActivityUnionRankListModel(activityId);
        for (Map.Entry<Integer, CrossGeneralActivityUnionRankModel> entry : model.entrySet()) {
            CrossGeneralActivityUnionRankModel rankListModel = entry.getValue();
            int conditionType = rankListModel.getConditionType();
            int rank = 0;
            for (CrossActivityUnionRank rankData : rankListModel.getRankList()) {
                String unionUid = rankData.getUnionUid();
                rank++;
                addUnionRankData(msg, conditionType, unionUid, rank);
            }
        }
        return msg;
    }


}
